<?php
class Post extends CI_Controller {

	private $header_data;

	function __construct()
	{
		parent::__construct();

		$this->load->library('session');
		$this->load->helper(array('form', 'url'));

		$this->load->model('Post_model');

		// for header welcome message
		if ($this->session->userdata('user'))
			$this->header_data['username'] = $this->session->userdata('user')->username;
		else
			$this->header_data['username'] = "khách";
	}

	function index()
	{
		$data['post'] = $this->Post_model->get_next_post();
		
		$this->load->view('header', $this->header_data);
		$this->load->view('post_view', $data);
		$this->load->view('footer');
	}
	
	// view post by id
	function view($id)
	{
		$data['post'] = $this->Post_model->get_post_by_id($id);
		$this->load->view('header', $this->header_data);
		$this->load->view('post_view', $data);
		$this->load->view('footer');
	}
	
	function vote($up_or_down, $id)
	{
		if ($up_or_down == "up")
			$score = 1;
		else if ($up_or_down == "down")
			$score = -1;
		$this->Post_model->rate($id, $score);
		echo "ok";
	}

	function ajax_get_next_post()
	{
		$data['post'] = $this->Post_model->get_next_post();
		echo( json_encode($data['post']));
	}
	
	// photo upload form 
	function create()
	{
		if (!$this->session->userdata('user')) { // not logged in?
			redirect('/user/create', 'location');
			return;
		}
			
		if (!$this->input->post('submit')) 
		{
			$this->load->view('header', $this->header_data);
			$this->load->view('post_create', array('error' => '', 'data' => ''));
			$this->load->view('footer');
		}
		else // process form
		{
			$this->load->library('upload');

			if (!$this->upload->do_upload('image_file'))
			{
				echo $this->upload->display_errors();
			}
			else
			{
				$width_l = 1200;
				$height_l = 900;
				$width_m = 800;
				$height_m = 600;
				$width_s = 100;
				
				$upload_data = $this->upload->data(); // get file details
				$filename = $upload_data['file_name'];
				$filename_l = $filename; // use original for large and medium if it's small enough
				$filename_m = $filename;

				$img_size = getimagesize('./media/'.$filename);

				$this->load->library('image_lib');
				$img_config['image_library'] = 'gd2';
				$img_config['source_image'] = './media/'.$filename;
				$img_config['master_dim'] = 'auto';
				$img_config['create_thumb'] = TRUE;
				$img_config['maintain_ratio'] = TRUE;
				
				// create large version
				if (($img_size[0] > $width_l) || ($img_size[1] > $height_l)) {
					$img_config['thumb_marker'] = '_l';
					$img_config['width'] = 1200;
					$img_config['height'] = 900;
					$this->image_lib->initialize($img_config); 
					if (!$this->image_lib->resize()) echo $this->image_lib->display_errors() . '\n';
					$filename_l = substr_replace($filename, '_l', strrpos($filename, "."), 0); // filename
				}

				// create medium version
				if (($img_size[0] > $width_m) || ($img_size[1] > $height_m)) {
					$img_config['thumb_marker'] = '_m';
					$img_config['width'] = $width_m;
					$img_config['height'] = $height_m;
					$this->image_lib->initialize($img_config); 
					if (!$this->image_lib->resize()) echo $this->image_lib->display_errors() . '\n';
					$filename_m = substr_replace($filename, '_m', strrpos($filename, "."), 0);
				}
				
				// create thumbnail
				$img_config['quality'] = '100%';
				$img_config['thumb_marker'] = '_s';
				$img_config['width'] = $width_s;
				$img_config['height'] = $width_s;
				$img_config['master_dim'] = (($img_size[0] > $img_size[1]) ? 'height' : 'width');
				$this->image_lib->initialize($img_config); 
				if (!$this->image_lib->resize()) echo $this->image_lib->display_errors() . '\n';
				
				// crop the thumbnail
				$filename_s = substr_replace($filename, '_s', strrpos($filename, "."), 0);
				$img_config['source_image'] = './media/'. $filename_s;
				$img_config['create_thumb'] = FALSE;
				$img_config['maintain_ratio'] = FALSE;
				if ($img_size[0] > $img_size[1]) // landscape
					$img_config['x_axis'] = round( ($width_s/2)*($img_size[0]/$img_size[1]-1) ); // offset horizontally
				else // portrait
					$img_config['y_axis'] = round( ($width_s/2)*($img_size[1]/$img_size[0]-1) ); // offset vertically
				$this->image_lib->initialize($img_config); 
				if (!$this->image_lib->crop()) echo $this->image_lib->display_errors() . '\n';
				
				$post_id = $this->Post_model->create($filename, $filename_l, $filename_m, $filename_s); // create db record				
				
				redirect('/post/view/'.$post_id, 'location');
			}
		}
	
	}

}
